<?php
namespace app\backstage\logic;
use think\Db;

class Agent
{
    /*列表数据获取*/
    public function getRechargeList($params)
    {
        $field = 'id,account,remarks';

        $params['role_id'] = 5;

        $condition[] = ['id', 'eq', 'gt:0','agent_id'];
        $condition[] = ['role_id', 'eq', 'gt:0','role_id'];

        $where   = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['id' => 'DESC'];
        }

        $Account = \think\facade\App::model('Account');
        $list = $Account->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($params['page'] == 1 )
        {
            if( !(isset($params['agent_id']) && $params['agent_id'] > 0) )
            {
                $list[] = ['id'=>0,'account'=>'平台所属','remarks'=>'未归属代理'];
            }
        }

        if($list)
        {

            $end_date = date('Y-m-d');
            $start_date = date('Y-m-d', strtotime('-6days'));

            $date_rang = getDateByRange($start_date, $end_date);

            foreach ($list as $k=>$v)
            {
                $recharge_amount_date = Db::table('rp_income_summary_daily_agent')
                    ->where('report_date','in',$date_rang)
                    ->where('agent_id',$v['id'])->column('report_date,recharge_amount');

                $list[$k][$date_rang[0]]    = isset($recharge_amount_date[$date_rang[0]]) ? getQuotient($recharge_amount_date[$date_rang[0]],100) : 0;
                $list[$k][$date_rang[1]]    = isset($recharge_amount_date[$date_rang[1]]) ? getQuotient($recharge_amount_date[$date_rang[1]],100) : 0;
                $list[$k][$date_rang[2]]    = isset($recharge_amount_date[$date_rang[2]]) ? getQuotient($recharge_amount_date[$date_rang[2]],100) : 0;
                $list[$k][$date_rang[3]]    = isset($recharge_amount_date[$date_rang[3]]) ? getQuotient($recharge_amount_date[$date_rang[3]],100) : 0;
                $list[$k][$date_rang[4]]    = isset($recharge_amount_date[$date_rang[4]]) ? getQuotient($recharge_amount_date[$date_rang[4]],100) : 0;
                $list[$k][$date_rang[5]]    = isset($recharge_amount_date[$date_rang[5]]) ? getQuotient($recharge_amount_date[$date_rang[5]],100) : 0;
                $list[$k][$date_rang[6]]    = isset($recharge_amount_date[$date_rang[6]]) ? getQuotient($recharge_amount_date[$date_rang[6]],100) : 0;
                $list[$k]['agent_name']     = strlen($v['remarks']) > 1 ? $v['account'].'（'.$v['remarks'].'）' : $v['account'];
            }
        }

        $result['count']    = $Account->getTotalNumber($where);
        $result['list']     = $list;
        return $result;
    }

}